package visaoInterface;

import dao.DaoTipoVeiculo;
import javax.swing.JOptionPane;
import modelo.dominio.TipoVeiculo;

public class ChamaTipoVeiculo extends javax.swing.JDialog {
    
    /**
     * Método que salva os dados no banco de dados a partir
     * do que o usuario digita nas caixas de texto.
     */
    public void salvaTipoVeiculos(){
        if(jTFTipoVeiculo.getText().equals("") || jTFTaxaBase.getText().equals("") || jTFPrecoKm.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Todos os campos devem ser preenchidos.");
        }else{
            TipoVeiculo tv = new TipoVeiculo();
            DaoTipoVeiculo dtv = new DaoTipoVeiculo();
            tv.setTipoVeiculo(jTFTipoVeiculo.getText());
            tv.setTaxaBase(Double.parseDouble(jTFTaxaBase.getText()));
            tv.setPrecoKm(Double.parseDouble(jTFPrecoKm.getText()));
            dtv.insere(tv, 0);
            limpaDadosCaixaTexto();
            JOptionPane.showMessageDialog(null, "Tipo de veiculo salvo com sucesso.");
        }
    }
    
    /**
     * Método que limpa os dados das caixas de texto na interface.
     */
    public void limpaDadosCaixaTexto(){
        jTFTipoVeiculo.setText("");
        jTFTaxaBase.setText("");
        jTFPrecoKm.setText("");    
    }
    
    /** Creates new form ChamaVeiculo */
    public ChamaTipoVeiculo(java.awt.Dialog parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLTipoVeiculo = new javax.swing.JLabel();
        jLTaxaBase = new javax.swing.JLabel();
        jLPrecoKm = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jBTVCancelar = new javax.swing.JButton();
        jBTVSalvar = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jTFTipoVeiculo = new javax.swing.JTextField();
        jTFTaxaBase = new javax.swing.JTextField();
        jTFPrecoKm = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Cadastro de Tipos de Veículo"));

        jLTipoVeiculo.setText("Tipo do Veículo");

        jLTaxaBase.setText("Taxa Base");

        jLPrecoKm.setText("Preço por Quilômetro");

        jLabel4.setText("R$");

        jLabel5.setText("R$");

        jBTVCancelar.setText("Cancelar");
        jBTVCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTVCancelarActionPerformed(evt);
            }
        });

        jBTVSalvar.setText("Salvar");
        jBTVSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTVSalvarActionPerformed(evt);
            }
        });

        jButton1.setText("Limpar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLPrecoKm)
                            .addComponent(jLTaxaBase)
                            .addComponent(jLTipoVeiculo))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTFTipoVeiculo, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                            .addComponent(jTFTaxaBase, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)
                            .addComponent(jTFPrecoKm, javax.swing.GroupLayout.DEFAULT_SIZE, 180, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(18, 18, 18)
                        .addComponent(jBTVSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(jBTVCancelar)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLTipoVeiculo)
                    .addComponent(jTFTipoVeiculo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLTaxaBase)
                    .addComponent(jLabel5)
                    .addComponent(jTFTaxaBase, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLPrecoKm)
                    .addComponent(jLabel4)
                    .addComponent(jTFPrecoKm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jBTVSalvar)
                    .addComponent(jBTVCancelar))
                .addGap(34, 34, 34))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBTVCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTVCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBTVCancelarActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        limpaDadosCaixaTexto();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jBTVSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTVSalvarActionPerformed
        salvaTipoVeiculos();
    }//GEN-LAST:event_jBTVSalvarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ChamaTipoVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ChamaTipoVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ChamaTipoVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ChamaTipoVeiculo.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                ChamaTipoVeiculo dialog = new ChamaTipoVeiculo(new javax.swing.JDialog(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBTVCancelar;
    private javax.swing.JButton jBTVSalvar;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLPrecoKm;
    private javax.swing.JLabel jLTaxaBase;
    private javax.swing.JLabel jLTipoVeiculo;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTextField jTFPrecoKm;
    private javax.swing.JTextField jTFTaxaBase;
    private javax.swing.JTextField jTFTipoVeiculo;
    // End of variables declaration//GEN-END:variables
}
